﻿/********************************************************************************
 * Developed by Phil Tyler (http://www.laidbackcoder.co.uk)
 * 
 * Cryptosolic is Licensed under the GNU Lesser General Public License (LGPL)
 * http://cryptosolic.codeplex.com/license
 * *****************************************************************************/
using System;
using System.Text;
using NUnit.Framework;
using Cryptosolic.Security.Hashing;

namespace Cryptosolic.Security.Tests
{
    [TestFixture]
    public class HashingAlgorithmTests
    {
        [Test]
        public void HashingAlgorithm_HashDataString_CallsHashDataBytes()
        {
            var hashingAlgorithmBase = new FakeHashingAlgorithm();

            hashingAlgorithmBase.HashData("this.is.a.test");

            Assert.IsTrue(hashingAlgorithmBase.WasCalled);
        }

        [Test]
        public void HashingAlgorithm_HashDataString_ConvertsDataToBytes()
        {
            var hashingAlgorithmBase = new FakeHashingAlgorithm();

            string initialData = "this.is.a.test";
            hashingAlgorithmBase.HashData(initialData);

            Assert.AreEqual(initialData, Encoding.UTF8.GetString(hashingAlgorithmBase.DataReceived));
        }

        [Test]
        public void HashingAlgorithm_HashDataStringWithIncrement_CallsHashDataBytesXTimes()
        {
            var hashingAlgorithmBase = new FakeHashingAlgorithm();
            int increments = 10;

            hashingAlgorithmBase.HashData("this.is.a.test", increments);

            Assert.AreEqual(increments, hashingAlgorithmBase.CallCount);
        }

        [Test]
        public void HashingAlgorithm_HashDataBytesWithIncrement_CallsHashDataBytesXTimes()
        {
            var hashingAlgorithmBase = new FakeHashingAlgorithm();
            int increments = 10;

            hashingAlgorithmBase.HashData(Encoding.UTF8.GetBytes("this.is.a.test"), increments);

            Assert.AreEqual(increments, hashingAlgorithmBase.CallCount);
        }
      
        class FakeHashingAlgorithm : HashingAlgorithm
        {
            
            public bool WasCalled = false;
            public byte[] DataReceived = null;
            public int CallCount = 0;

            public override CipherText HashData(byte[] data)
            {
                WasCalled = true;
                DataReceived = data;
                CallCount++;
                return new CipherText(data);
            }
        }
    }
}
